package com.linln.admin.member.controller;

import com.linln.admin.member.validator.MemberInfoValid;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.StatusUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.member.domain.MemberInfo;
import com.linln.modules.member.domain.MemberOrder;
import com.linln.modules.member.service.MemberInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hct
 * @date 2019/09/12
 */
@Controller
@RequestMapping("/member/memberInfo")
public class MemberInfoController {

    @Autowired
    private MemberInfoService memberInfoService;

    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("member:memberInfo:index")
    public String index(Model model,String startTime,String endTime,String nickname,String mobile,Integer status) {

        
        Page<MemberInfo> list = memberInfoService.getPageList(startTime,endTime, nickname,mobile, status);

        // 封装数据
        model.addAttribute("list", list.getContent());
        model.addAttribute("page", list);
        return "/member/memberInfo/index";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    @RequiresPermissions("member:memberInfo:add")
    public String toAdd() {
        return "/member/memberInfo/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("member:memberInfo:edit")
    public String toEdit(@PathVariable("id") MemberInfo memberInfo, Model model) {
        model.addAttribute("memberInfo", memberInfo);
        return "/member/memberInfo/add";
    }

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping("/save")
    @RequiresPermissions({"member:memberInfo:add", "member:memberInfo:edit"})
    @ResponseBody
    public ResultVo save(@Validated MemberInfoValid valid, MemberInfo memberInfo) {
        // 复制保留无需修改的数据
        if (memberInfo.getId() != null) {
            MemberInfo beMemberInfo = memberInfoService.getById(memberInfo.getId());
            EntityBeanUtil.copyProperties(beMemberInfo, memberInfo);
        }

        // 保存数据
        memberInfoService.save(memberInfo);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("member:memberInfo:detail")
    public String toDetail(@PathVariable("id") MemberInfo memberInfo, Model model) {
        model.addAttribute("memberInfo",memberInfo);
        return "/member/memberInfo/detail";
    }

    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping("/status/{param}")
    @RequiresPermissions("member:memberInfo:status")
    @ResponseBody
    public ResultVo status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Long> ids) {
        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        if (memberInfoService.updateStatus(statusEnum, ids)) {
            return ResultVoUtil.success(statusEnum.getMessage() + "成功");
        } else {
            return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
        }
    }
    
    
    /**
     * 列表页面
     */
    @RequestMapping("/search")
    @RequiresPermissions("member:memberInfo:index")
    @ResponseBody
    public ResultVo search(Model model,String keywords) {

    	 // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching();
        MemberInfo memberInfo=new MemberInfo();
        memberInfo.setNickname(keywords);
        matcher.withMatcher("nickname", match -> match.startsWith());
        
        // 获取数据列表
        Example<MemberInfo> example = Example.of(memberInfo, matcher);

        Page<MemberInfo> list = memberInfoService.getPageList(null,null, keywords,null, null);
        return ResultVoUtil.success(list.getContent());

    }
}